package model;

import java.util.Arrays;
import java.util.List;

import org.mybeans.dao.DAOException;
import org.mybeans.factory.BeanFactory;
import org.mybeans.factory.BeanFactoryException;
import org.mybeans.factory.BeanTable;
import org.mybeans.factory.MatchArg;
import org.mybeans.factory.RollbackException;
import org.mybeans.factory.Transaction;

import dataBeans.User;
import exceptions.MyWebsiteException;

public class UserDAO {
	private BeanFactory<User> userlist;

	public UserDAO() throws DAOException{
		try {
			// Get a BeanTable so we can create the "user" table
			BeanTable<User> userTable = BeanTable.getInstance(User.class,"mkhani_user");

			if (!userTable.exists()){ 
				userTable.create("userId");
			}
			userTable.setIdleConnectionCleanup(true);
			userlist = userTable.getFactory();
		} catch (BeanFactoryException e) {
			throw new DAOException(e);
		}
	}
	
	public UserDAO(String jdbcDriver, String jdbcURL, String tableName){
		try{
			BeanTable<User> userTable = BeanTable.getSQLInstance(User.class, tableName, jdbcDriver, jdbcURL);
			if(!userTable.exists()){
				userTable.create("userId");
			}
			userlist = userTable.getFactory();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public UserDAO(String jdbcDriverName, String jdbcURL, String tableName, String dbUsername, String dbPassword){
		try{
			BeanTable<User> userTable = BeanTable.getSQLInstance(User.class, tableName, jdbcDriverName, jdbcURL, dbUsername, dbPassword);
			if(!userTable.exists()){
				userTable.create("userId");
			}
			userlist = userTable.getFactory();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public User create(String firstName, String lastName, String email, String password) throws MyWebsiteException{
		try{
			if(lookup(email) == null){
				Transaction.begin();
				User userDb = userlist.create();
				userDb.setEmail(email);
				userDb.setFirstName(firstName);
				userDb.setLastName(lastName);
				userDb.setPassword(password);
				Transaction.commit();
			}else{
				throw new MyWebsiteException("The user already exists");
			}
		}catch(Exception e){
			e.printStackTrace();
			throw new MyWebsiteException("");
		}
		return new User(firstName, lastName, email, password);
	}
	
	public User create(User user2Insert) throws MyWebsiteException{
		try{
			Transaction.begin();
			User userDb = userlist.create();
			userDb.setEmail(user2Insert.getEmail());
			userDb.setFirstName(user2Insert.getFirstName());
			userDb.setLastName(user2Insert.getLastName());
			userDb.setPassword(user2Insert.getPassword());
			Transaction.commit();		
		}catch(Exception e){
			e.printStackTrace();
			throw new MyWebsiteException("Can't Login");
		}
		return user2Insert;
	}
	
	public User lookup(String email) throws MyWebsiteException{
		try{
			User internalUserList[] = userlist.match(MatchArg.equals("email", email));
			if(internalUserList.length > 0)
				return internalUserList[0];
			else
				return null;
		}catch(RollbackException e){
			e.printStackTrace();
			throw new MyWebsiteException("can't login");
		}
	}
	
	public User lookup2(String firstName)throws MyWebsiteException{
		try{
			User internalUserList[] = userlist.match(MatchArg.equals("firstName", firstName));
			return internalUserList[0];
		}catch(RollbackException e){
			e.printStackTrace();
			throw new MyWebsiteException("can't login");
		}
	}
	
	public User login(String email, String password) throws MyWebsiteException{
		//System.out.println("I am here");
		try{
			 User [] loginUser = userlist.match(MatchArg.equals("email", email));
			 if(loginUser != null && loginUser.length > 0){
				 if(loginUser[0].getPassword().equals(password)){
					 return loginUser[0];
				 }else{
					 throw new MyWebsiteException("invalid login credentials"); 
				 }
			 }else{
				 throw new MyWebsiteException("you're unregistered user");
			 }
		}catch(RollbackException e){
			e.printStackTrace();
			throw new MyWebsiteException("can't login");
		}
	}
	
	public User [] getUserList() throws MyWebsiteException{
		try{
			System.out.println("I am in the UserDAO and trying to get the users");
			User [] userArray = userlist.match();
			if(userArray != null){
				System.out.println("the length of the UserArray" + userArray.length);
			}else{
				System.out.println("No Users in the Database");
			}
			return userArray;
		}catch(Exception e){
			e.printStackTrace();
			throw new MyWebsiteException("Can't get the user list");
		}
	}
	
	public User changePassword(int userId, String password) throws MyWebsiteException{
		try{
			Transaction.begin();
			User currentUser = userlist.lookup(userId);
			System.out.println("I got the User");
			if(currentUser != null){
				currentUser.setPassword(password);
				Transaction.commit();
				return currentUser;
			}else{
				throw new MyWebsiteException("invalid user ID");
			}
		}catch(RollbackException e){
			e.printStackTrace();
			throw new MyWebsiteException("RollBackException");
		}
	}
}
